package com.lian.aieduc.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.lian.aieduc.utils.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.util.EntityUtils;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/courses")
@RequiredArgsConstructor()
@Tag(name = "课程相关接口", description = "课程相关API接口")
public class CourseController {

    private final RestTemplate restTemplate;
    private final String url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";
    private final String apiKey = "284ee67f-ba45-4ef2-af19-af55ebafc055";

    private static Map<String, Object> getStringObjectMap() {
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("model", "ep-20241108122529-rclqw");

        List<Map<String, String>> messages = new ArrayList<>();
//        Map<String, String> systemMessage = new HashMap<>();
//        systemMessage.put("role", "system");
//        systemMessage.put("content", "你有什么需要帮助的吗.");
//        messages.add(systemMessage);
        Map<String, String> userMessage = new HashMap<>();
        userMessage.put("role", "user");
        userMessage.put("content", "如何让自己开心起来");
        messages.add(userMessage);

        requestBody.put("messages", messages);
//        requestBody.put("stream", true);
        return requestBody;
    }

    @GetMapping("/paged")
    public Result<String> getCourses() {
        return Result.success("");
    }

    @GetMapping("/ai")
    public JSONObject chatAI(String content) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // 如果有授权信息（类似之前的Bearer token），添加如下
        headers.set("Authorization", "Bearer 284ee67f-ba45-4ef2-af19-af55ebafc055");
        // 设置接受的编码格式为UTF-8
//        headers.set("Accept-Encoding", "UTF-8");
        // 设置发送的数据格式为UTF-8的JSON
//        headers.setContentType(MediaType.APPLICATION_JSON);

        System.out.println(headers);
        Map<String, Object> requestBody = getStringObjectMap();

        System.out.println(requestBody);

        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);

        String json = new String(restTemplate.postForObject(url, entity, byte[].class), StandardCharsets.UTF_8);

        JSONObject jsonObject = JSON.parseObject(json);

        log.info("jsonObject:{}",jsonObject);


        return jsonObject;
    }

}
